package com.ddyx.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ddyx.entity.Activity;
import com.ddyx.entity.ActivityGoods;
import com.ddyx.entity.StoreActivity;
import com.ddyx.entity.user.UEmploye;
import com.ddyx.mapper.ActivityGoodsMapper;
import com.ddyx.mapper.ActivityMapper;
import com.ddyx.mapper.GoodsMapper;
import com.ddyx.mapper.StoreActivityMapper;
import com.ddyx.service.StoreActivityService;
import com.ddyx.util.MyBeanUtils;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Slf4j
@Service
public class StoreActivityServiceImpl implements StoreActivityService {

	@Resource
	StoreActivityMapper StoreActivityMapper;
	
	@Resource
	ActivityGoodsMapper activityGoodsMapper;
	@Resource
	GoodsMapper goodsMapper;
	
	@Override
	public ResponseForm getActivitysByParams(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (null != param) {
			StoreActivity bean = new StoreActivity();
			Map<String, Object> paramMap = (Map<String, Object>) param.getData();
			try {
				
				paramMap = com.ddyx.util.Page.parsePage(paramMap);
				Example example = new Example(StoreActivity.class);
				String sort= (String) paramMap.get("sort");
				
				if(sort!=null && "1".equals(sort)){
					example.setOrderByClause("create_time DESC");
				}else{
					example.setOrderByClause("sort DESC");
				}
				
				
				Criteria c = example.createCriteria();
				c.andEqualTo("storeNo", paramMap.get("storeNo"));
				c.andEqualTo("type", paramMap.get("type"));
				c.andEqualTo("category", paramMap.get("category"));
				c.andEqualTo("status", 0);
//				String searchName= (String) paramMap.get("searchName");
//				if(StringUtils.isNotBlank(searchName)){
//					c.orLike("employeNo", "%"+ searchName +"%");
//					c.orLike("mobile", "%"+ searchName +"%");
//					c.orLike("employeName", "%"+ searchName +"%");
//					
//				}
					
				Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
				List<StoreActivity> list=StoreActivityMapper.selectByExample(example);
				result.setData(list);
				result.setTotal((int) pageHelper.getTotal());
				
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}

	@Override
	public ResponseForm updateActivity(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (null != param) {
			StoreActivity bean = new StoreActivity();
			Map<String, Object> paramMap = (Map<String, Object>) param.getData();
			try {
				MyBeanUtils.populate(bean, paramMap);
				
				List<String> goodsList = (List) paramMap.get("goodsList");
				
				//删除操作标识
				
				String flag = (String) paramMap.get("flag");
				if(flag!=null && flag.equals("delete")){
					
					StoreActivityMapper.updateByPrimaryKeySelective(bean);
					
				}else{
					if(bean.getLink()==1||bean.getLink()==3){
						if(goodsList.size()==0){
							result.setStatus(false);
							result.setMessage("商品不能为空");
							return result;
						}
					}
					
					//bean.setStatus(0);
					bean.setCreateTime(new Date());
					StoreActivityMapper.updateByPrimaryKeySelective(bean);
					
					//活动与商品对应
					if(bean.getLink()==1||bean.getLink()==3){
						if( goodsList.size()>0){
							Example example = new Example(ActivityGoods.class);
							Criteria c = example.createCriteria();
							c.andEqualTo("activityId",bean.getId() );
							activityGoodsMapper.deleteByExample(example);
							ActivityGoods agBean;
							for(int i =0;i<goodsList.size();i++){
								agBean=new ActivityGoods();
								agBean.setActivityId(bean.getId());
								Object goodsId= goodsList.get(i);
								agBean.setGoodsId((Integer) goodsId);
								activityGoodsMapper.insert(agBean);
							}
						}
						
					}
				}
				
				
				
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
		
	}
	@Transactional
	@Override
	public ResponseForm addActivity(RequestForm param) {
		
		ResponseForm result = new ResponseForm();
		if (null != param) {
			StoreActivity bean = new StoreActivity();
			Map<String, Object> paramMap = (Map<String, Object>) param.getData();
			try {
				MyBeanUtils.populate(bean, paramMap);
				
				List<String> goodsList = (List) paramMap.get("goodsList");
				
				if(bean.getLink()==1||bean.getLink()==3){
					if(goodsList.size()==0){
						result.setStatus(false);
						result.setMessage("商品不能为空");
						return result;
					}
				}
				
				//bean.setStatus(0);
				bean.setCreateTime(new Date());
				StoreActivityMapper.insertSelective(bean);
				
				//活动与商品对应
				if(bean.getLink()==1||bean.getLink()==3){
					if( goodsList.size()>0){
						ActivityGoods agBean;
						for(int i =0;i<goodsList.size();i++){
							agBean=new ActivityGoods();
							agBean.setActivityId(bean.getId());
							Object goodsId= goodsList.get(i);
							agBean.setGoodsId((Integer) goodsId);
							activityGoodsMapper.insert(agBean);
						}
					}
					
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
		
	}

	@Override
	public ResponseForm getActivityById(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (null != param) {
			StoreActivity bean = new StoreActivity();
			Map<String, Object> paramMap = (Map<String, Object>) param.getData();
			try {
				MyBeanUtils.populate(bean, paramMap);
				
				bean =StoreActivityMapper.selectOne(bean);
				
				if(bean.getLink()==1 ||bean.getLink()==3){
					List aList =goodsMapper.getActivityGoodsList(bean.getId());
					bean.setGoodsList(aList);
				}
				
				result.setData(bean);
				
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}

	@Override
	public ResponseForm addActivityCheck(RequestForm param) {
		// TODO Auto-generated method stub
		return null;
	}

}
